package org.lex.lang;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.apache.axis.encoding.Base64;

public class PersistHelper {

	public String encode(Serializable object) throws IOException {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		ObjectOutputStream oout = new ObjectOutputStream(bout);
		oout.writeObject(object);
		oout.flush();
		oout.close();
		return Base64.encode(bout.toByteArray());
	}

	public Object decode(String base64) throws IOException, ClassNotFoundException {
		byte[] data = Base64.decode(base64);
		ByteArrayInputStream bis = new ByteArrayInputStream(data);
		ObjectInputStream ois = new ObjectInputStream(bis);
		Object o = ois.readObject();
		ois.close();
		return o;
	}
}
